<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/26
 * Time: 17:54
 */
require_once ("conf.php");
require_once ("lib/Snoopy.class.php");
require_once ("lib/func.php");

$snoopy = new Snoopy();


/*

$shopify_products[] = [
    'variant_id' => 13769313288251,
    'quantity'   => 1,
];
$shopify_products[] = [
    'variant_id' => 13869026246715,
    'quantity'   => 2,
];
$shopify_products[] = [
    'variant_id' => 13869045710907,
    'quantity'   => 3,
];

$shopify_order = [];
$shopify_order['order'] = [
    'line_items'      => $shopify_products,
    'billing_address' => ["first_name" => "charles",
      "last_name"    => "chen",
      "address1"     => "bao an",
      "phone"        => "13728770334",
      "city"         => "Shenzhen",
      "province"     => "Guangdong",
      "country"      => "China",
      "zip"          => "518000"
    ],
    'shipping_address' => [
    "first_name" => "Jane",
      "last_name" =>  "Smith",
      "address1" =>  "123 Fake Street",
      "phone" =>  "777-777-7777",
      "city" => "Fakecity",
      "province" =>  "Ontario",
      "country" => "Canada",
      "zip" => "K2P 1L4"
    ],
    "customer" => [
        'id'   => '727911694395',
    ],
    'financial_status'   => 'paid',
    'fulfillment_status' => 'fulfilled',
    'inventory_behaviour' => 'decrement_obeying_policy',

];

$url = getApiUrl("/admin/orders.json");
$data = postJSON($url, json_encode($shopify_order));
file_put_contents("order.txt", $data);


*/

$db_eachvape = getEachVapeDb();
$db_shopify  = getShopifyDb();

$sql_order = "select order_id , order_status_id from oc_order where order_status = '" . ORDER_PROCCED . "' AND  order_id = 31";
$rs_order  = mysqli_query($db_eachvape, $sql_order);
while($row_order = mysqli_fetch_assoc($rs_order)){
    $order_status_id = $row_order['order_status_id'];
    $order_id = $row_order["order_id"];
    if($order_status_id == ORDER_PROCCED){
        $sql_order_status = "SELECT * FROM order_sync where order_id = '" . $order_id ."'";
        $rs_order_status  = mysqli_query($db_shopify,$sql_order_status);
        $row_order_status = mysqli_fetch_assoc($rs_order_status);
        if($row_order_status){
            if($row_order_status['sync_status'] != 3){
                shopifyOrderSync($order_id);
            }
        }else{
            shopifyOrderSync($order_id);
        }
    }
}

